﻿using Bricks.Business.Service;
using Bricks.Component.Utils.Const;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils.Extensions;
using Bricks.Site.Models;
using System.Text;
using Bricks.Business.Service.CommonSupport;
using Bricks.Component.Utils;
using Bricks.Site.Web.CommonUtils;
using System.Xml;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class StatisticsItemMaintenanceController : BaseController
    {
        [Import]
        public IStatisticsItemMaintenanceService StatisticsItemMaintenanceService { get; set; }

        public ActionResult StatisticsItemMaintenanceList()
        {
            ViewBag.ToolBarInfo = base.GetToolBarInfo();
            return View();
        }

        public ActionResult GetStatisticsItemMaintenanceList(string projectID, string contractType)
        {
            if (string.IsNullOrEmpty(projectID) || string.IsNullOrEmpty(contractType))
            {
                return Json(new List<StatisticsItemMaintenanceView>(), JsonRequestBehavior.AllowGet);
            }
            else
            {
                Guid guidprojectID = projectID.ToGuid();
                return Json(StatisticsItemMaintenanceService.GetStatisticsItemMaintenanceList(guidprojectID, contractType), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult GetParentStatisticsItemMaintenanceList(string projectID, string contractType)
        {
            Guid guidprojectID = projectID.ToGuid();
            return Json(StatisticsItemMaintenanceService.GetParentStatisticsItemMaintenanceList(guidprojectID, contractType), JsonRequestBehavior.AllowGet);
        }



        public ActionResult GetStatisticsItemMaintenance(string projectID, string contractType, string id)
        {
            Guid guidID;
            if (!String.IsNullOrEmpty(id))
            {
                guidID = id.ToGuid();
            }
            else
            {
                guidID = Guid.NewGuid();
            }

            StatisticsItemMaintenanceView model = StatisticsItemMaintenanceService.GetStatisticsItemMaintenance(guidID);
            if (model == null)
            {
                model = new StatisticsItemMaintenanceView();
                model.ID = guidID;
                model.ProjectID = projectID.ToGuid();
                model.ContractType = Server.UrlDecode(contractType);
                model.IsParent = true;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult IsBeRef(StatisticsItemMaintenanceView model)
        {
            return Content(StatisticsItemMaintenanceService.IsBeRef(model));
        }
        
        [HttpPost]
        public ActionResult SaveStatisticsItemMaintenance(StatisticsItemMaintenanceView model)
        {
            if (model.PID == Guid.Empty)
            {
                model.PID = null;
            }
            return Json(StatisticsItemMaintenanceService.SaveStatisticsItemMaintenance(model), "text/html");
        }

        [HttpPost]
        public ActionResult SaveStatisticsItemMaintenanceAndDeleteRefData(StatisticsItemMaintenanceView model)
        {
            if (model.PID == Guid.Empty)
            {
                model.PID = null;
            }
            return Json(StatisticsItemMaintenanceService.SaveStatisticsItemMaintenanceAndDeleteRefData(model), "text/html");
        }

        public ActionResult DeleteStatisticsItemMaintenance(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = StatisticsItemMaintenanceService.Delete(guidID);
            return Json(exeResult);
        }


        public ActionResult CopyStatisticsItemMaintenance()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SaveCopyStatisticsItemMaintenance(string projectID, string contractType, string copyProjectID, string copyContractType)
        {
            return Json(StatisticsItemMaintenanceService.SaveCopyStatisticsItemMaintenance(projectID.ToGuid(), contractType, copyProjectID.ToGuid(), copyContractType), "text/html");
        }


        [HttpPost]
        public ActionResult SetStageOrder(string id, string order)
        {
            Guid guidID = id.ToGuid();
            var exeResult = StatisticsItemMaintenanceService.SetOrder(guidID, order);
            return Json(exeResult);
        }


        public ActionResult PreviewStatisticsItemMaintenance(string id)
        {
            StatisticsItemMaintenanceView model = StatisticsItemMaintenanceService.GetStatisticsItemMaintenance(id.ToGuid());
            return View(model);
        }
    }
}
